package org.example;

import instrument.MD5Util;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        AdminService admin = new AdminService();
        ManagerService manager = new ManagerService(admin);
        ReceptionService reception = new ReceptionService(manager);
        Scanner sc = new Scanner(System.in);  // 统一Scanner实例

        while (true) {
            System.out.println("\n=== 系统主菜单 ===");
            System.out.println("1.管理员登录 2.经理登录 3.前台登录 4.退出");
            switch (sc.nextLine().trim()) {
                case "1" -> {
                    System.out.print("用户名: ");
                    String user = sc.nextLine();
                    System.out.print("密码: ");
                    String pwd = sc.nextLine();
                    if (admin.login(user, pwd)) {
                        admin.menu();
                    } else {
                        System.out.println("登录失败");
                    }
                }
                case "2" -> {
                    System.out.print("用户名: ");
                    String user = sc.nextLine();
                    System.out.print("密码: ");
                    String pwd = sc.nextLine();
                    User u = admin.getUsers().stream()
                            .filter(x -> x.getUsername().equals(user)
                                    && x.getPassword().equals(MD5Util.encrypt(pwd))
                                    && x.getUserType().equals("经理"))
                            .findFirst().orElse(null);
                    if (u != null) {
                        manager.setCurrentManager(u);
                        manager.showManagerMenu();
                    } else {
                        System.out.println("登录失败");
                    }
                }
                case "3" -> {
                    System.out.print("用户名: ");
                    String user = sc.nextLine();
                    System.out.print("密码: ");
                    String pwd = sc.nextLine();
                    User u = admin.getUsers().stream()
                            .filter(x -> x.getUsername().equals(user)
                                    && x.getPassword().equals(MD5Util.encrypt(pwd))
                                    && x.getUserType().equals("前台"))
                            .findFirst().orElse(null);
                    if (u != null) {
                        reception.setCurrentReception(u);
                        reception.showReceptionMenu();
                    } else {
                        System.out.println("登录失败");
                    }
                }
                case "4" -> {
                    sc.close();
                    return;
                }
                default -> System.out.println("无效选择");
            }
        }
    }
}